package com.ceres.farm.service.impl;

import com.ceres.crops.pojo.Crops;
import com.ceres.farm.dao.*;
import com.ceres.farm.pojo.*;
import com.ceres.farm.service.FieldService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@SuppressWarnings("all")
public class FieldServiceImpl implements FieldService {

    @Autowired
    CustFarmlandMapper custFarmlandMapper;

    /**
     * 大田的添加
     * @param custFarmland
     */
    @Override
    public void add(CustFarmland custFarmland) {
        int insert = custFarmlandMapper.insert(custFarmland);
        if(insert==0){
            throw new RuntimeException("添加失败！");
        }
    }

    /**
     * 大田的分页查询
     * @param pageNumber
     * @param pageSize
     * @param aid
     * @param keyword
     * @return
     */
    @Override
    public Page<CustFarmland> pageQuery(Integer pageNumber, Integer pageSize, Integer aid, String keyword) {

        PageHelper.startPage(pageNumber,pageSize);
        Page<CustFarmland> page=(Page<CustFarmland>) custFarmlandMapper.findByKeyWord(keyword);
        return page;
    }

    /**
     * 大田农作物
     */
    @Autowired
    CustFarmlandCropsMapper custFarmlandCropsMapper;
    @Autowired
    CustFarmMapper custFarmMapper;
    @Autowired
    CropsMapper cropsMapper;
    //大田设备
    @Autowired
    CustFarmlandDeviceMapper deviceMapper;
    /**
     * 大田详情
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> queryById(Integer id) {
        //创建Map用于储存相关信息
        Map<String,Object> map=new HashMap<>();
        //根据大田ID查询片区id
       Integer aid= custFarmlandMapper.findIdById(id);
       //根据片区ID查询片区名称
        CustFarm custFarm = custFarmMapper.selectByPrimaryKey(aid);
        //添加片区名称
        map.put("aName",custFarm.getName());
        //根据大田ID查询农作物
        CustFarmlandCrops custFarmlandCrops = custFarmlandCropsMapper.findNameById(id);
        //添加农作物名称
        map.put("cname",custFarmlandCrops.getCname());
        //根据农作物名称和生长状态查询相关指标
            //根据农作物名称和生长状态查询指标
        List< Map<String,Object>> cropsMapperIndexList = cropsMapper.findIndex(custFarmlandCrops.getCname(), custFarmlandCrops.getStage());
            //指标map，用于存放指标数据
        List<Map<String,String>> list=new ArrayList<>();
        for (Map<String, Object> cropsMapperIndexmap : cropsMapperIndexList) {
            //遍历map集合
            for (String s : cropsMapperIndexmap.keySet()) {
                if("id".equals(s)){
                    //获取联合表的ID
                    Integer cropid =(Integer) cropsMapperIndexmap.get(s);
                    //根据ID查询指标名称
                    String indexType = cropsMapper.findIndexType(cropid);
                    Map<String,String> indexMap=new HashMap<>();
                    indexMap.put("indexType",indexType);
                    indexMap.put("range",cropsMapperIndexmap.get("opt_range").toString());
                    list.add(indexMap);
                }
            }
        }
            //添加指标数据
        map.put("crops",list);
        //根据农作物查询害虫
        List<String> pestList = cropsMapper.findPest(custFarmlandCrops.getCname());
        map.put("pests",pestList);
        //根据大田查询设备信息
        List<CustFarmlandDevice> deviceList = deviceMapper.selectByFid(id);
        //添加设备数量
        map.put("deviceNum",deviceList.size());
        //添加设备集合
        map.put("devices",deviceList);
        //根据大田ID查询大田基本信息
        CustFarm custFarm1 = custFarmMapper.selectByPrimaryKey(id);
        map.put("custFarml",custFarm1);
        return map;
    }

    /**
     * 修改大田信息
     * @param custFarmland
     */
    @Override
    public void update(CustFarmland custFarmland) {
        int i = custFarmlandMapper.updateByPrimaryKeySelective(custFarmland);
        if(i==0){
            throw new RuntimeException("修改失败");
        }
    }
    //历史数据表
    @Autowired
    HistoryMapper historyMapper;
    //查询大田历史信息
    @Override
    public List<History> findHistory(Integer fid) {
      List<History> historyList=  historyMapper.findAllByFid(fid);
        return historyList;
    }
}
